// pages/equement_info/equement_info.js
var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
var lockjs = require('../../assets/js/database/lock.js')
var base = require('../../assets/js/base.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    modalInput: true,
    token: '',
    randomKey: '',
    sign: '',
    userId: '',

    equeInfo: [],      //设备

    lockArray: [],     //用户锁列表
    endLockArray: [],  //设备锁列表
    addressName: '',   //用户输入的地址
  },

  
    
  openAlert: function () {
    var that = this;
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
    that.getLockByUser(that.data.userId, that.data.currentPage);
    that.getLockByEqu(that.data.equeInfo.id, '0000')
    
  },
  //取消按钮
  cancel: function () {
    this.setData({ hiddenmodalput: true });
    console.log('用户点击取消')
  },
  //确认    
  confirm: function () {
    this.setData({ hiddenmodalput: true })
    console.log('用户点击确认')
  },
  //地址取消按钮
  cancelInput: function () {
    this.setData({ modalInput: true });
    console.log('用户点击取消')
  },
  //地址确认    
  confirmInput: function () {
    var that = this;
    this.setData({ modalInput: true })
    that.uploadEque(that.data.equeInfo.id, that.data.addressName, '', '')
  },
  /**
   * 获取当前用户拥有的锁
   */
  getLockByUser: function (userId, offset){
      var that = this;
      var simpleObject = {
        id: userId,
        offset: offset,
        limit: getApp().globalData.limit
      };
      var param = common.packParam(that.data.randomKey, simpleObject);
      var url = getApp().globalData.httpUrl + '/lock/getLockByUser';
      var info = wxRequest.postRequest(url, param, that.data.token).
        then(res => {
          var array = res.data;
          console.log(res.data)
          that.setData({
            lockArray: res.data
          })
        })
  },
  /**
   * 获取该设备的锁
   */
  getLockByEqu: function (equementId, offset){
      var that = this;
      var simpleObject = {
        id: equementId,
        offset: offset,
        limit: getApp().globalData.limit
      };
      var param = common.packParam(that.data.randomKey, simpleObject);
      //获取锁信息
      var url = getApp().globalData.httpUrl + '/lock/getLockByEquement';
      var info = wxRequest.postRequest(url, param, that.data.token).
        then(res => {
          that.setData({
            endLockArray: res.data
          })
        })
  },
  /**
   * 把锁绑定设备
   */
  bindLock: function (lockCode, eID){
    var that = this;
    var simpleObject = {
      id: eID,
      lockCode: lockCode,
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    //获取锁信息
    var url = getApp().globalData.httpUrl + '/lock/bindLock';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        
      })
  },
  /**
   * 把锁解除绑定设备
   */
  bindDelLock: function (lockCode, eID) {
    var that = this;
    var simpleObject = {
      id: eID,
      lockCode: lockCode,
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    //获取锁信息
    var url = getApp().globalData.httpUrl + '/lock/bindDelLock';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {

      })
  },
  /**
   * 锁具选择  点击左边框
   */
  lockClick1: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    var data = {
      endLockArray: that.data.endLockArray,
      lockArray: that.data.lockArray
    };
    var lockCode = data.lockArray[index].lockCode;
    data.endLockArray.push(data.lockArray[index])  //添加到右边数组
    data.lockArray.splice(index, 1)                 //删除左边数组元素
    
    this.setData(data)
    /**
     * 锁具绑定设备
     */
    that.bindLock(lockCode, that.data.equeInfo.id)
  },
  /**
   * 锁具选择  点击右边框
   */
  lockClick2: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    var data = {
      endLockArray: that.data.endLockArray,
      lockArray: that.data.lockArray
    };
    var lockCode = data.endLockArray[index].lockCode;
    data.lockArray.push(data.endLockArray[index])  //添加到右边数组
    data.endLockArray.splice(index, 1)                 //删除左边数组元素

    this.setData(data)
    /**
     * 锁具解除绑定设备
     */
    that.bindDelLock(lockCode, that.data.equeInfo.id)
  },
  upAddress: function(){
    this.setData({
      modalInput: !this.data.modalInput
    })
   
  },
  /**
   * 更新设备
   */
  uploadEque: function (equementId, address, latitude, longitude){
    var that = this;
    var simpleObject = {
      id: equementId,
      address: base.utf16to8(address),
      latitude: latitude,
      longitude: longitude
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    //更新坐标信息
    var url = getApp().globalData.httpUrl + '/equipment/update';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        if (parseInt(res.data) >= 1) {
          wx.showToast({
            title: '已更新',
            icon: 'success',
            duration: 3000
          });
        }
      })
  },
  /**
   * 查看位置
   */
  lookLocation: function(){
    var that = this;
    var info = that.data.equeInfo
    var latitude = parseFloat(info.latitude)
    var longitude = parseFloat(info.longitude)
    console.log(latitude)
    console.log(longitude)
    
    wx.openLocation({
      latitude,
      longitude,
      scale: 28
    })
  },
  /**
   * 更新设备的坐标和地址
   */
  chLocation: function(){
    var that = this;
    wx.chooseLocation({
      success(res) { 
        const name = res.name
        const address = res.address
        const latitude = res.latitude
        const longitude = res.longitude
        console.log(name)
        console.log(address)
        console.log(latitude)
        console.log(longitude)
        that.uploadEque(that.data.equeInfo.id, address, latitude, longitude) //更新坐标
        
      }
    })
  },
  /**
   * 获取用户输入的地址
   */
  inputTyping: function(e){
    var that = this;
    that.setData({
      addressName: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
    // ---------------------
    if (options.equeInfo) {
      var info = JSON.parse(options.equeInfo);
      that.setData({
        equeInfo: info
      })
      console.log(info)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})